package quq.missq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import quq.missq.BaseActivity;
import quq.missq.R;
/**
 * Created by wcx on 2015/9/17.
 */
public class ModifyInfoActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_have;
    private RelativeLayout rl_have;
    private ToggleButton toggle_key;
    private EditText et_editor_top;
    private EditText et_editor_bottom;
    private TextView tv_charnumber;
    private TextView tv_cancel;
    private TextView tv_desc;
    private TextView tv_confirm;
    private int type;
    private Intent data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modifyinfo);
        initView();
        initData();
        initListener();
    }

    private int cou = 0;
    int selectionEnd = 0;
    private int mMaxLenth=40;//限制字数为40
    public  String stringFilter(String str)throws PatternSyntaxException {
        String regEx = "[/\\:*?<>|\"\n\t]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("");
    }


    private boolean isOpen=false;

    private void initListener() {
        tv_cancel.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        et_editor_bottom.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cou = before + count;
                String editable = et_editor_bottom.getText().toString();
                String str = stringFilter(editable);
                if (!editable.equals(str)) {
                    et_editor_bottom.setText(str);
                }
                et_editor_bottom.setSelection(et_editor_bottom.length());
                cou = et_editor_bottom.length();
                tv_charnumber.setText(s.length() + "");
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (cou > mMaxLenth) {
                    selectionEnd = et_editor_bottom.getSelectionEnd();
                    s.delete(mMaxLenth, selectionEnd);
                }
            }
        });

        toggle_key.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.i("wcx",isChecked+"toggle_key");
                isOpen=isChecked;
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if(intent.hasExtra("type")){
            type = intent.getExtras().getInt("type");
            switch (type){
                case 0://修改昵称
                    ll_have.setVisibility(View.VISIBLE);
                    tv_desc.setText("昵称");
                    break;
                case 1://修改姓名
                    ll_have.setVisibility(View.VISIBLE);
                    toggle_key.setVisibility(View.VISIBLE);
                    tv_desc.setText("姓名");
                    break;
                case 2://修改个性签名
                    rl_have.setVisibility(View.VISIBLE);
                    tv_desc.setText("个性签名");
                    break;
            }
        }
    }

    private void initView() {
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_desc = (TextView) findViewById(R.id.tv_desc);
        tv_confirm = (TextView) findViewById(R.id.tv_confirm);
        ll_have = (LinearLayout) findViewById(R.id.ll_have);
        rl_have = (RelativeLayout) findViewById(R.id.rl_have);
        toggle_key = (ToggleButton) findViewById(R.id.toggle_key);
        et_editor_top = (EditText) findViewById(R.id.et_editor_top);
        et_editor_bottom = (EditText) findViewById(R.id.et_editor_bottom);
        tv_charnumber = (TextView) findViewById(R.id.tv_charnumber);
    }


    private int getType(boolean flag){
        if(flag){
            return 1;//公开
        }else{
            return 2;//未公开
        }
    }

    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.tv_cancel:
                 finish();
                 break;
             case R.id.tv_confirm:
                 switch (type){
                     case 0:
                         String text_Name1= et_editor_top.getText().toString();
                         data = new Intent();
                         data.putExtra("Name", text_Name1);
                         setResult(RESULT_OK, data);
                         finish();
                     case 1:
                         String text_Name2= et_editor_top.getText().toString();
                         data = new Intent();
                         data.putExtra("Name", text_Name2);
                         data.putExtra("namePrivacy",getType(isOpen));
                         setResult(RESULT_OK, data);
                         finish();
                         break;
                     case 2:
                         String pen_Name= et_editor_bottom.getText().toString();
                         Intent data = new Intent();
                         data.putExtra("pen_Name", pen_Name);
                         setResult(RESULT_OK, data);
                         finish();
                         break;
                 }
                 break;
         }
    }
}
